import collections


class WordDictionary(object):

    def __init__(self):
        self.words_dict = collections.defaultdict(set)

    def addWord(self, word):
        """
        :type word: str
        :rtype: None
        """
        length = len(word)
        for i in range(length):
            self.words_dict[(length, i, word[i])].add(word)
            self.words_dict[(length, i, '.')].add(word)

    def search(self, word):
        """
        :type word: str
        :rtype: bool
        """
        length = len(word)
        now_set = set()
        for i in range(len(word)):
            if not now_set:
                now_set = self.words_dict[(length, i, word[i])]
                if not now_set:
                    return False
            else:
                now_set = now_set & self.words_dict[(length, i, word[i])]
                if not now_set:
                    return False
        return True


obj = WordDictionary()
obj.addWord('a')
obj.addWord('ab')
print(obj.search('..'))